Skip to content

Conversation

teresaromero
Copy link
Contributor

@teresaromero teresaromero commented Sep 25, 2025

Resolves #2797

Summary

Refactored the linked files system to use explicit os.Root parameters for secure repository access. This change fixes a bug where linked files were copied to incorrect directories and reveals a secondary issue where external fields were resolved before linked files were included in the build bundle.

Problems Fixed

  1. Linked files copied to wrong directory: The workDir in LinksFS was treated inconsistently (absolute vs relative), causing incorrect path resolution. Now standardized to always be relative to repository root.

  2. External fields resolved too early: The build process resolved external fields before copying linked files, causing missing field definitions. Reordered build steps to copy linked files first.

Key Changes

Repository Root Handling:

  • CreateLinksFSFromPath() now requires explicit *os.Root parameter
  • All callers must find and pass repository root explicitly
  • Added proper cleanup with defer repositoryRoot.Close()

API Changes:

  • FindPackageRoot() signature: (string, bool, error)(string, error)
  • Added ErrPackageRootNotFound sentinel error
  • BuildOptions.PackageRoot renamed to PackageRootPath and added RepositoryRoot field
  • Package installer Options.RootPath renamed to PackageRootPath and added RepositoryRoot field

Build Process:

  • License file resolution now uses repository root securely
  • Linked files included before resolving external fields
  • ELASTIC_PACKAGE_REPOSITORY_LICENSE now expects paths relative to repository root

Breaking Changes

Functions that previously auto-discovered repository root now require it as a parameter:

  • CreateLinksFSFromPath(repositoryRoot, workDir)
  • BuildPackage() requires RepositoryRoot in options
  • NewForPackage() requires RepositoryRoot in options

Testing

  • All existing tests updated and passing
  • New test coverage for license file handling and edge cases
  • Manual verification confirms fixes for both bugs

@teresaromero teresaromero marked this pull request as ready for review September 30, 2025 13:01
@teresaromero
Copy link
Contributor Author

@mrodm i've updated the code with your feedback. i usually mark resolved the comments that have been addressed, please re-open if you find anything pending.

thanks

Copy link
Member

@jsoriano jsoriano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks great, it seems to solve links resolution and does a good refactor to allow injection of the path to the root repository.

I did some tests and found some issues, commented in the files, though maybe some of them can be solved in follow ups.

Many comments are nitpicking, no need to do anything about them if you prefer current code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried to link a fields file that includes fields with external: ecs and elastic-package fails to resolve these fields when building the package, with errors like:

Error: building package failed: invalid content found in built zip package: found 7 validation errors:
   1. file ".../github.com/elastic/integrations/build/packages/system-2.6.1.zip/data_stream/cpu/fields/agent.yml" is invalid: field cloud.account.id with external key defined ("ecs") but no _dev/build/build.yml found

I guess it is trying to read the build.yml file from the target directory instead of from the source directory. It only happens with linked files.

Not sure if the issue is introduced with the current PR, so we can handle this in a follow up, as you prefer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you walk me through this test? i am not sure i understood correctly the operation here.

Create a .link file for ecs.yml of one package ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be reproduced with the following commands, from the integrations repository:

mkdir packages/system/_dev/shared
mv packages/system/data_stream/cpu/fields/agent.yml packages/system/_dev/shared/agent.yml
echo ../../../_dev/shared/agent.yml > packages/system/data_stream/cpu/fields/agent.yml.link
elastic-package links update
elastic-package build -C packages/system

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been able to reproduce this. The cause i've found is that, when the build command is resolving the external references, the link files are not yet at the build directory of the package. The linked files are added to the build bundle after, so when validation is run, the file for agent.yml has not been transformed/resolved and is the original one.

I've changed the order of the steps to solve this. So now links are resolved, and so, the linked files are correctly copied into the bundle. After this, the external references are resolved correctly.

@elasticmachine
Copy link
Collaborator

elasticmachine commented Oct 21, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor links resolver so it can write to files out of the repository

4 participants